#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* s)
{
	assert(s);
	s->a = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
	if (s->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	s->size = 0;
	s->capcity = INIT_CAPACITY;
}
void SLDestroy(SL* s)
{
	free(s->a);
	s->a = NULL;
	s->size = 0;
	s->capcity = INIT_CAPACITY;
}

void  SLCheck(SL* s)
{
	if (s->size == s->capcity)
	{
		SLDataType* tmp = (SLDataType*)realloc(s->a, sizeof(SLDataType) * (s->capcity * 2));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		s->a = tmp;
		s->capcity *= 2;
	}
}

//void SLpushback(SL* s, SLDataType x)
//{
//	assert(s);
//	SLCheck(s);
//	s->a[s->size++] = x;
//}

void SLpushback(SL* s, SLDataType x)
{
	assert(s);
	SLCheck(s);
	s->a[s->size++] = x;
}

//void SLpushfront(SL* s, SLDataType x)
//{
//	assert(s);
//	SLCheck(s);
//	int n = s->size;
//	while (n)
//	{
//		s->a[n] = s->a[n - 1];
//		n--;
//	}
//	s->a[0] = x;
//	s->size++;
//}
void SLpushfront(SL* s, SLDataType x)
{
	assert(s);
	SLCheck(s);
	SLInsert(s, 0, x);
}

void SLpopback(SL* s)
{
	assert(s->size > 0 &&s);
	s->size--;
}

void SLpopfront(SL* s)
{
	assert(s->size>0 && s);
	for (int i = 0; i < s->size-1; i++)
	{
		s->a[i] = s->a[i + 1];
	}
	s->size--;
}

void SLPrint(SL* s)
{
	assert(s);
	for (int i = 0; i < s->size; i++)
	{
		printf("%d ", s->a[i]);
	}
	printf("\n");
}

int SLFind(SL* s, SLDataType x)
{
	assert(s);
	for (int i = 0; i < s->size; i++)
	{
		if (x == s->a[i])
		{
			return i;
		}
	}
	return 0;
}

void SLInsert(SL* s, int pos, SLDataType x)
{
	assert(s);
	assert(0 <= pos && pos <= s->size);
	SLCheck(s);
	int end = s->size - 1;
	while (end >= pos)
	{
		s->a[end + 1] = s->a[end];
		end--;
	}
	s->a[pos] = x;
	s->size++;
}

void SLErase(SL* s, int pos)
{
	assert(s);
	assert(0 <= pos && pos < s->size);
	int p = pos;
	while (p< s->size-1)
	{
		s->a[p] = s->a[p + 1];
		p++;
	}
	s->size--;
}